package No1_100.No21_30.MergeTwoSortedLists_21;

import No1_100.No1_10.AddTwoNumbers_2.ListNode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    	ListNode ret = new ListNode(0);
    	ListNode head = ret;
        while (l1 != null&&l2 != null) {
			if (l1.val<=l2.val) {
				ret.next = l1;
				l1 = l1.next;
			}
			else {
				ret.next = l2;
				l2 = l2.next;
			}
			ret = ret.next;
		}
        if (l1 != null) {
			ret.next = l1;
		}
        if (l2 != null) {
			ret.next = l2;
		}
    	
    	return head.next;
    }
    public static void main(String[] args) {
    	ListNode lsListNode = new ListNode(0);
		ListNode retListNode = lsListNode;
		lsListNode.val = 3;
		ListNode retListNode2 = new ListNode(0);
		retListNode2.val = 9;
		lsListNode.next = retListNode2;
		
		ListNode retListNode3 = new ListNode(0);
		retListNode3.val = 6;
		ListNode ret = mergeTwoLists(retListNode3,retListNode);
		while(ret!=null){
			System.out.print(ret.val);
			ret = ret.next;
		}
		
		System.out.println("\nok");
	}
}